package com.qf.agro.service.device.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.agro.mapper.device.SmartDeviceDataLogsMapper;
import com.qf.agro.pojo.device.SmartDeviceDataLogs;
import com.qf.agro.service.device.SmartDeviceDataLogsService;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author: 六六六
 * @create: 2023-07-04 14:32
 **/
@Service
public class SmartDeviceDataLogsServiceImpl implements SmartDeviceDataLogsService {

    @Autowired
    private SmartDeviceDataLogsMapper smartDeviceDataLogsMapper;
    @Override
    public Page<SmartDeviceDataLogs> findPage(Integer pageNum, Integer pageSize, Long facilitiesId, Long deviceId, Integer day, Integer resultCode) {
        Page<SmartDeviceDataLogs> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<SmartDeviceDataLogs> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(facilitiesId!=null,SmartDeviceDataLogs::getFacilitiesId,facilitiesId);
        queryWrapper.eq(deviceId!=null,SmartDeviceDataLogs::getDeviceId,deviceId);
        if (day!=null) {
            queryWrapper.ge(SmartDeviceDataLogs::getCreateTime, DateUtils.addDays(new Date(),-day));
        }
        queryWrapper.eq(SmartDeviceDataLogs::getResultCode,resultCode);
        Page<SmartDeviceDataLogs> smartDeviceDataLogsPage = smartDeviceDataLogsMapper.selectPage(page, queryWrapper);
        return smartDeviceDataLogsPage;
    }

    @Override
    public List<SmartDeviceDataLogs> findList(Long facilitiesId, Long deviceId, Integer day, Integer resultCode) {
       LambdaQueryWrapper<SmartDeviceDataLogs> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(facilitiesId!=null,SmartDeviceDataLogs::getFacilitiesId,facilitiesId);
        queryWrapper.eq(deviceId!=null,SmartDeviceDataLogs::getDeviceId,deviceId);
        queryWrapper.eq(SmartDeviceDataLogs::getResultCode,resultCode);
        if (day != null) {
            long currentTimeMillis = System.currentTimeMillis();
            long millis=day*24*60*60*1000;
            long dateTimeMillis = currentTimeMillis - millis;
            Date date = new Date(dateTimeMillis);
            queryWrapper.ge(SmartDeviceDataLogs::getCreateTime,date);
        }
        List<SmartDeviceDataLogs> smartDeviceDataLogs = smartDeviceDataLogsMapper.selectList(queryWrapper);
        return smartDeviceDataLogs;
    }

    @Override
    public void addDeviceDataLogs(SmartDeviceDataLogs smartDeviceDataLogs) {
        smartDeviceDataLogsMapper.insert(smartDeviceDataLogs);
    }
}
